import { GenericEnvStore } from "@/modules/dev-env/store/generic-env-store.ts";
import type { LocalVersionItem, ToolInstallVersionItem } from "@/modules/dev-env/common/types.ts";
import type { BaseEnvMirror } from "@/modules/dev-env/mirror/mirror.ts";
import { NacosStableMirror } from "@/modules/dev-env/middleware/nacos/nacos-mirror.ts";
import type { UnzipCompleteArgs } from "@common/archives-file-types.ts";
import path from "@/framework/util/path.ts";
import type { EnvTableAction } from "@/modules/dev-env/store/types.ts";
import { hc } from "@/framework/vue/util/render.ts";
import { PlayCircleFilled } from "@ant-design/icons-vue";
import { execInTerminal } from "@/util/system-utils2.ts";

export class NacosEnvStore extends GenericEnvStore<NacosEnvStore> {
  constructor() {
    super("nacos");

    this.pageConfig = {
      showSetting: false,
      showCurrentAction: false,
    };

    this.tableActions = this._tableActions();
  }

  private _tableActions(): EnvTableAction[] {
    return [
      {
        key: "nacos.start",
        name: "启动",
        icon: hc(PlayCircleFilled, { class: "color-success" }),
        onClick: it => this._startupStandalone(it),
      },
    ];
  }

  private _startupStandalone(version: LocalVersionItem) {
    execInTerminal(`startup.cmd -m standalone`, {
      cwd: version.installPath,
      subPath: ["bin"],
    });
  }

  protected getMirrors0(): BaseEnvMirror[] {
    return [new NacosStableMirror()];
  }

  protected getVersionInfo(versionDir: string): Promise<LocalVersionItem | undefined> {
    const version = path.basename(versionDir);
    return Promise.resolve({
      version: version,
      installPath: versionDir,
    });
  }

  async afterUnzipComplete(e: UnzipCompleteArgs, version: ToolInstallVersionItem) {
    const outputDir = path.join(e.fileDir, "nacos");
    const versionDir = path.join(e.fileDir, version.version);
    await native.fs.rename(outputDir, versionDir);
  }
}
